package hz.mobileproject.pda.ui.activity.work;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.broadcast.HandleBroadcast;
import hz.mobileproject.pda.broadcast.IShowBarcodeStr;
import hz.mobileproject.pda.entity.Photo;
import hz.mobileproject.pda.ui.adapter.DeptCheckGVAdapter;
import hz.mobileproject.pda.ui.view.CustomPopWindow;
import hz.mobileproject.pda.ui.view.LoadingDialog;
import hz.mobileproject.pda.ui.view.PhotoDialog;
import hz.mobileproject.pda.utils.TakePhotoUtil;

/**
 * Created by hasee on 2017/11/16.
 * 部门检查
 */
public class DepartmentCheckActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener, IShowBarcodeStr {
    private TextView tv_check_result;
    private LinearLayout ll_check_error;
    private GridView gv_photo;
    private TextView tv_police_code;

    private DeptCheckGVAdapter mAdapter;
    /**
     * 照片相关-------------------------------------------
     */
    //当前照片路径
    private String mImageFilePath;
    /**
     * 扫码相关-------------------------------------------
     */
    private HandleBroadcast handleBroadcast;
    private SoundPool soundpool;
    private int soundid;
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_department_check);
        initView();
        initData();
        initThings();
    }

    private void initView() {
        tv_check_result = (TextView) findViewById(R.id.tv_check_result);
        ll_check_error = (LinearLayout) findViewById(R.id.ll_check_error);
        gv_photo = (GridView) findViewById(R.id.gv_photo);
        tv_police_code = (TextView) findViewById(R.id.tv_police_code);
    }

    private void initData() {
        mAdapter = new DeptCheckGVAdapter(this,new ArrayList<Photo>());
        initScan();
    }

    private void initScan() {
        handleBroadcast = new HandleBroadcast(this, this);
        soundpool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 100); // MODE_RINGTONE
        soundid = soundpool.load("/etc/Scan_new.ogg", 1);
        loadingDialog = new LoadingDialog(this);
        loadingDialog.setMessage("请扫描民警二维码").show();
    }

    private void initThings() {
        gv_photo.setAdapter(mAdapter);
        tv_check_result.setOnClickListener(this);
        gv_photo.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.tv_check_result://选择检查结果
                showPopListView(tv_check_result,new String[]{"较好","正常","异常"});
                break;
        }
    }




    private void showPopListView(final View v, String[] args){
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_custom_list,null);
        //处理popWindow 显示内容
        ListView listView = (ListView) contentView.findViewById(R.id.lv_dialog);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_pop_inlv, args);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            }
        });
        //创建并显示popWindow
        final CustomPopWindow mListPopWindow= new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)
                .size(v.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT)
                .create()
                .showAsDropDown(v,0,10);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                mListPopWindow.dissmiss();
                ((TextView)v).setText(((TextView)view).getText().toString());
                if(i==2){
                    ll_check_error.setVisibility(View.VISIBLE);
                }else{
                    ll_check_error.setVisibility(View.INVISIBLE);
                }
            }
        });
    }


    /**
     * gridview 点击事件
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //当点击下标大于等于list size时   为添加照片
        if(position >= mAdapter.getListData().size()){
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            final CharSequence[] items = {"拍照","从相册中选择"};
            dialog.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(which == 0){
                        TakePhotoUtil.getInstance(DepartmentCheckActivity.this).takePhoto();
                    }else if(which == 1){
                        TakePhotoUtil.getInstance(DepartmentCheckActivity.this).openPic();
                    }
                }
            });
            //显示提示框
            dialog.show();
        }else{
            //点击下标小于list sizi时  查看照片
            Photo photo = mAdapter.getItem(position);
            PhotoDialog dialog = new PhotoDialog(this,photo.getPath());
            dialog.show();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            mImageFilePath = TakePhotoUtil.getInstance(DepartmentCheckActivity.this).getCurPicPath(requestCode,data);
            LogUtils.e("照片路径:"+mImageFilePath);
            if(StringUtils.isEmpty(mImageFilePath)){
                ToastUtils.showLong("照片路径错误");
                return;
            }
            mAdapter.add(new Photo(mImageFilePath));
        }
    }


    /**
     * 扫码回调
     * @param msg
     */
    @Override
    public void updateText(String msg) {
        LogUtils.e(msg);
        soundpool.play(soundid, 1, 1, 0, 0, 1);
    }
}
